/*
 * MiniPaint4View.java
 */
package minipaint4Vista;

import MiniPaint4Logica.RepositorioPaint;
import MiniPaint4Infraestructura.RepositorioEnBBDD;
import MiniPaint4Logica.DatosDeFigura;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.PopupFactory;

/**
 * The application's main frame.
 */
public class MiniPaint4View extends FrameView {

    String figuraSeleccionada = "";
    List<DatosDeFigura> datos = new ArrayList();
    RepositorioPaint almacen = new RepositorioEnBBDD();

    public MiniPaint4View(SingleFrameApplication app) {
        super(app);

        initComponents();
        
        Guardar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                almacen.guardarLista(NombreDibujo.getText(), datos);                                                      
            }
        });
        
        superficieDibujo.setEnabled(false);
        
        Nuevo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                superficieDibujo.setEnabled(true);
                superficieDibujo.removeAll();
                datos.clear();
                superficieDibujo.repaint();
            }
        });
        
        Cargar.setText("Cargar");
        
        Cargar.addActionListener(new ActionListenerCargar(almacen, superficieDibujo));
        
        
        Init.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                almacen.inicializar();
            }
        });

        selectorLinea.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (selectorLinea.isSelected()) {
                    figuraSeleccionada = "Linea";
                }
            }
        });

        selectorRectangulo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (selectorRectangulo.isSelected()) {
                    figuraSeleccionada = "Rectangulo";
                }
            }
        });

        selectorCirculo.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (selectorCirculo.isSelected()) {
                    figuraSeleccionada = "Circulo";
                }
            }
        });

        superficieDibujo.addMouseListener(new MouseListener() {

            boolean esPrimerClick = true;
            Point puntoUno = null;

            public void mouseClicked(MouseEvent e) {


                if (esPrimerClick) {
                    puntoUno = e.getPoint();
                    esPrimerClick = false;
                } else {
                    if (figuraSeleccionada.equals("Linea")) //Me sabe mal hacer esto con if/if, y estoy convencido de que se podría hacer con polimorfismo pero... si supiera cómo lo habría hecho.
                    superficieDibujo.add(new Linea(puntoUno, e.getPoint(), superficieDibujo.getVisibleRect(), datos));
                    if (figuraSeleccionada.equals("Rectangulo"))
                    superficieDibujo.add(new Rectangulo(puntoUno, e.getPoint(), superficieDibujo.getVisibleRect(), datos));
                    if (figuraSeleccionada.equals("Circulo"))
                    superficieDibujo.add(new Circulo(puntoUno, e.getPoint(), superficieDibujo.getVisibleRect(), datos));
                    esPrimerClick = true;
                    superficieDibujo.repaint();
                }

            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });


    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = MiniPaint4App.getApplication().getMainFrame();
            aboutBox = new MiniPaint4AboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        MiniPaint4App.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        superficieDibujo = new javax.swing.JPanel();
        selectorLinea = new javax.swing.JRadioButton();
        selectorRectangulo = new javax.swing.JRadioButton();
        selectorCirculo = new javax.swing.JRadioButton();
        NombreDibujo = new javax.swing.JTextField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        Nuevo = new javax.swing.JMenuItem();
        Guardar = new javax.swing.JMenuItem();
        Cargar = new javax.swing.JMenuItem();
        Init = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();
        selectoresFigura = new javax.swing.ButtonGroup();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(minipaint4Vista.MiniPaint4App.class).getContext().getResourceMap(MiniPaint4View.class);
        superficieDibujo.setBackground(resourceMap.getColor("superficieDibujo.background")); // NOI18N
        superficieDibujo.setName("superficieDibujo"); // NOI18N

        javax.swing.GroupLayout superficieDibujoLayout = new javax.swing.GroupLayout(superficieDibujo);
        superficieDibujo.setLayout(superficieDibujoLayout);
        superficieDibujoLayout.setHorizontalGroup(
            superficieDibujoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 325, Short.MAX_VALUE)
        );
        superficieDibujoLayout.setVerticalGroup(
            superficieDibujoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 275, Short.MAX_VALUE)
        );

        selectoresFigura.add(selectorLinea);
        selectorLinea.setText(resourceMap.getString("selectorLinea.text")); // NOI18N
        selectorLinea.setName("selectorLinea"); // NOI18N

        selectoresFigura.add(selectorRectangulo);
        selectorRectangulo.setText(resourceMap.getString("selectorRectangulo.text")); // NOI18N
        selectorRectangulo.setName("selectorRectangulo"); // NOI18N

        selectoresFigura.add(selectorCirculo);
        selectorCirculo.setText(resourceMap.getString("selectorCirculo.text")); // NOI18N
        selectorCirculo.setName("selectorCirculo"); // NOI18N

        NombreDibujo.setText(resourceMap.getString("NombreDibujo.text")); // NOI18N
        NombreDibujo.setName("NombreDibujo"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(selectorLinea)
                            .addComponent(selectorRectangulo)
                            .addComponent(selectorCirculo))
                        .addGap(11, 11, 11)
                        .addComponent(superficieDibujo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addComponent(NombreDibujo, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(130, 130, 130))))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(NombreDibujo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(superficieDibujo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(selectorLinea)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectorRectangulo)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectorCirculo)))
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        Nuevo.setText(resourceMap.getString("Nuevo.text")); // NOI18N
        Nuevo.setName("Nuevo"); // NOI18N
        fileMenu.add(Nuevo);

        Guardar.setText(resourceMap.getString("Guardar.text")); // NOI18N
        Guardar.setName("Guardar"); // NOI18N
        fileMenu.add(Guardar);

        Cargar.setText(resourceMap.getString("Cargar.text")); // NOI18N
        Cargar.setName("Cargar"); // NOI18N
        fileMenu.add(Cargar);

        Init.setText(resourceMap.getString("Init.text")); // NOI18N
        Init.setName("Init"); // NOI18N
        fileMenu.add(Init);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(minipaint4Vista.MiniPaint4App.class).getContext().getActionMap(MiniPaint4View.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 261, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Cargar;
    private javax.swing.JMenuItem Guardar;
    private javax.swing.JMenuItem Init;
    private javax.swing.JTextField NombreDibujo;
    private javax.swing.JMenuItem Nuevo;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JRadioButton selectorCirculo;
    private javax.swing.JRadioButton selectorLinea;
    private javax.swing.JRadioButton selectorRectangulo;
    private javax.swing.ButtonGroup selectoresFigura;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JPanel superficieDibujo;
    // End of variables declaration//GEN-END:variables
    private JDialog aboutBox;
}
